<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>《属性数据分析》代码</title>
  <meta name="description" content="《属性数据分析》代码。">
  <meta name="generator" content="bookdown 0.7 and GitBook 2.6.7">

  <meta property="og:title" content="《属性数据分析》代码" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="《属性数据分析》代码。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="《属性数据分析》代码" />
  
  <meta name="twitter:description" content="《属性数据分析》代码。" />
  



<meta name="date" content="2018-09-05">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="index.html">
<link rel="next" href="contingency-table.html">
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/gitbook/css/style.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./index.html">《属性数据分析》代码</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>前言</a></li>
<li class="chapter" data-level="1" data-path="intro.html"><a href="intro.html"><i class="fa fa-check"></i><b>1</b> 导言</a><ul>
<li class="chapter" data-level="1.1" data-path="intro.html"><a href="intro.html#data-intro"><i class="fa fa-check"></i><b>1.1</b> 属性响应数据</a></li>
<li class="chapter" data-level="1.2" data-path="intro.html"><a href="intro.html#prob-dist"><i class="fa fa-check"></i><b>1.2</b> 属性数据的概率分布</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布计算"><i class="fa fa-check"></i>二项分布计算</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="intro.html"><a href="intro.html#stat-infer"><i class="fa fa-check"></i><b>1.3</b> 比例的统计推断</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布似然函数图"><i class="fa fa-check"></i>二项分布似然函数图</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布假设检验"><i class="fa fa-check"></i>二项分布假设检验</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布置信区间"><i class="fa fa-check"></i>二项分布置信区间</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="intro.html"><a href="intro.html#more-stat-infer"><i class="fa fa-check"></i><b>1.4</b> 关于离散数据的更多统计推断</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布参数统计推断"><i class="fa fa-check"></i>二项分布参数统计推断</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#小样本推断"><i class="fa fa-check"></i>小样本推断</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#小样本推断p值调整"><i class="fa fa-check"></i>小样本推断P值调整</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#problems-ch1"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#第4题"><i class="fa fa-check"></i>第4题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="contingency-table.html"><a href="contingency-table.html"><i class="fa fa-check"></i><b>2</b> 列联表</a><ul>
<li class="chapter" data-level="2.1" data-path="contingency-table.html"><a href="contingency-table.html#stucture"><i class="fa fa-check"></i><b>2.1</b> 列联表的概率结构</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#关于来世"><i class="fa fa-check"></i>关于来世</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="contingency-table.html"><a href="contingency-table.html#prop-compare"><i class="fa fa-check"></i><b>2.2</b> 2×2表比例的比较</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#阿司匹林与心脏病列联表检验"><i class="fa fa-check"></i>阿司匹林与心脏病（列联表检验）</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="contingency-table.html"><a href="contingency-table.html#odds-ratio"><i class="fa fa-check"></i><b>2.3</b> 优势比</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#阿司匹林与心脏病优势比"><i class="fa fa-check"></i>阿司匹林与心脏病（优势比）</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#吸烟状态与心肌梗死"><i class="fa fa-check"></i>吸烟状态与心肌梗死</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="contingency-table.html"><a href="contingency-table.html#chi-square-test"><i class="fa fa-check"></i><b>2.4</b> 独立性的卡方检验</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#性别和党派认同"><i class="fa fa-check"></i>性别和党派认同</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="contingency-table.html"><a href="contingency-table.html#indenpendence-test-for-ordinal-data"><i class="fa fa-check"></i><b>2.5</b> 有序数据的独立性检验</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#饮酒与婴儿畸形"><i class="fa fa-check"></i>饮酒与婴儿畸形</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="contingency-table.html"><a href="contingency-table.html#exact-test-for-small-sample"><i class="fa fa-check"></i><b>2.6</b> 小样本的精确推断</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#女士品茶"><i class="fa fa-check"></i>女士品茶</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="contingency-table.html"><a href="contingency-table.html#three-way-table"><i class="fa fa-check"></i><b>2.7</b> 三项列联表的关联性</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#死刑判决案例"><i class="fa fa-check"></i>死刑判决案例</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#临床试验"><i class="fa fa-check"></i>临床试验</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#problems-ch2"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第18题"><i class="fa fa-check"></i>第18题</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第22题"><i class="fa fa-check"></i>第22题</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第33题"><i class="fa fa-check"></i>第33题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="glm.html"><a href="glm.html"><i class="fa fa-check"></i><b>3</b> 广义线性模型</a><ul>
<li class="chapter" data-level="3.1" data-path="glm.html"><a href="glm.html#components-of-glm"><i class="fa fa-check"></i><b>3.1</b> 广义线性模型的构成部分</a></li>
<li class="chapter" data-level="3.2" data-path="glm.html"><a href="glm.html#glm-for-binary-data"><i class="fa fa-check"></i><b>3.2</b> 二分数据的广义线性模型</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#打鼾与心脏病"><i class="fa fa-check"></i>打鼾与心脏病</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="glm.html"><a href="glm.html#glm-for-count-data"><i class="fa fa-check"></i><b>3.3</b> 计数数据的广义线性模型</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#母鲎及其追随者泊松glm"><i class="fa fa-check"></i>母鲎及其追随者（泊松GLM）</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#母鲎及其追随者负二项glm"><i class="fa fa-check"></i>母鲎及其追随者（负二项GLM）</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#英国的火车事故"><i class="fa fa-check"></i>英国的火车事故</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="glm.html"><a href="glm.html#stat-infer-glm"><i class="fa fa-check"></i><b>3.4</b> 统计推断和模型检验</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#打鼾与心脏病-1"><i class="fa fa-check"></i>打鼾与心脏病</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="glm.html"><a href="glm.html#fit-glm"><i class="fa fa-check"></i><b>3.5</b> 广义线性模型的拟合</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#problems-ch3"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第3题"><i class="fa fa-check"></i>第3题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第4题-1"><i class="fa fa-check"></i>第4题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第7题"><i class="fa fa-check"></i>第7题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第20题"><i class="fa fa-check"></i>第20题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="logistic-regression.html"><a href="logistic-regression.html"><i class="fa fa-check"></i><b>4</b> logistic回归</a><ul>
<li class="chapter" data-level="4.1" data-path="logistic-regression.html"><a href="logistic-regression.html#interpret-logistic"><i class="fa fa-check"></i><b>4.1</b> logistic回归模型的解释</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#母鲎及其追随者logistic回归"><i class="fa fa-check"></i>母鲎及其追随者（logistic回归）</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="logistic-regression.html"><a href="logistic-regression.html#infer-logistic"><i class="fa fa-check"></i><b>4.2</b> logistic回归的推断</a></li>
<li class="chapter" data-level="4.3" data-path="logistic-regression.html"><a href="logistic-regression.html#cate-var-logistic"><i class="fa fa-check"></i><b>4.3</b> 属性预测变量的logistic回归</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#azt和aids"><i class="fa fa-check"></i>AZT和AIDS</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="logistic-regression.html"><a href="logistic-regression.html#multi-logistic"><i class="fa fa-check"></i><b>4.4</b> 多元logistic回归</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#母鲎及其追随者多元logistic"><i class="fa fa-check"></i>母鲎及其追随者（多元logistic）</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="logistic-regression.html"><a href="logistic-regression.html#logistic回归效应的概括"><i class="fa fa-check"></i><b>4.5</b> logistic回归效应的概括</a></li>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#problem-ch4"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#第8题"><i class="fa fa-check"></i>第8题</a></li>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#第24题"><i class="fa fa-check"></i>第24题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html"><i class="fa fa-check"></i><b>5</b> logistic回归模型的构建和应用</a><ul>
<li class="chapter" data-level="5.1" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#model-selection"><i class="fa fa-check"></i><b>5.1</b> 模型选择策略</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者模型选择"><i class="fa fa-check"></i>母鲎及其追随者（模型选择）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者预测功效"><i class="fa fa-check"></i>母鲎及其追随者（预测功效）</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#model-checking"><i class="fa fa-check"></i><b>5.2</b> 模型检验</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者模型lr检验"><i class="fa fa-check"></i>母鲎及其追随者（模型LR检验）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#azt和aids拟合优度"><i class="fa fa-check"></i>AZT和AIDS（拟合优度）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者hm检验"><i class="fa fa-check"></i>母鲎及其追随者（HM检验）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#佛罗里达大学研究生入学"><i class="fa fa-check"></i>佛罗里达大学研究生入学</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#心脏病与血压的关系"><i class="fa fa-check"></i>心脏病与血压的关系</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#sparse-data-logistic"><i class="fa fa-check"></i><b>5.3</b> 稀疏数据效应</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#稀疏数据的临床试验结果"><i class="fa fa-check"></i>稀疏数据的临床试验结果</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#conditional-logistic"><i class="fa fa-check"></i><b>5.4</b> 条件logistic回归与精确推断</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#晋升能力"><i class="fa fa-check"></i>晋升能力</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#logistic-sample-num"><i class="fa fa-check"></i><b>5.5</b> logistic回归的样本量与功效</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#样本量计算"><i class="fa fa-check"></i>样本量计算</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#problem-ch5"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第10题"><i class="fa fa-check"></i>第10题</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第18题-1"><i class="fa fa-check"></i>第18题</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第28题"><i class="fa fa-check"></i>第28题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="multi-logit-model.html"><a href="multi-logit-model.html"><i class="fa fa-check"></i><b>6</b> 多类别logit模型</a><ul>
<li class="chapter" data-level="6.1" data-path="multi-logit-model.html"><a href="multi-logit-model.html#nomial-logit"><i class="fa fa-check"></i><b>6.1</b> 名义响应变量的logit模型</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#钝吻鳄食物选择"><i class="fa fa-check"></i>钝吻鳄食物选择</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#是否相信来世"><i class="fa fa-check"></i>是否相信来世</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="multi-logit-model.html"><a href="multi-logit-model.html#ordinal-logit"><i class="fa fa-check"></i><b>6.2</b> 有序响应变量的累积logit模型</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#政治意识形态和隶属党派的关系"><i class="fa fa-check"></i>政治意识形态和隶属党派的关系</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#对心理健康建模"><i class="fa fa-check"></i>对心理健康建模</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="multi-logit-model.html"><a href="multi-logit-model.html#paired-ordinal-logit"><i class="fa fa-check"></i><b>6.3</b> 成对类别有序logit</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#再访政治意识形态"><i class="fa fa-check"></i>再访政治意识形态</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#发育毒性研究"><i class="fa fa-check"></i>发育毒性研究</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="multi-logit-model.html"><a href="multi-logit-model.html#conditional-independent"><i class="fa fa-check"></i><b>6.4</b> 条件独立性检验</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#工作满意度和收入"><i class="fa fa-check"></i>工作满意度和收入</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#ch6-problems"><i class="fa fa-check"></i>课后题</a></li>
</ul></li>
<li class="appendix"><span><b>附录</b></span></li>
<li class="chapter" data-level="A" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html"><i class="fa fa-check"></i><b>A</b> 配套R包使用介绍</a><ul>
<li class="chapter" data-level="A.1" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html#r-pkg-install"><i class="fa fa-check"></i><b>A.1</b> 安装</a></li>
<li class="chapter" data-level="A.2" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html#r-pkg-use"><i class="fa fa-check"></i><b>A.2</b> 使用说明</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="book-dataset-list.html"><a href="book-dataset-list.html"><i class="fa fa-check"></i><b>B</b> 教材数据列表</a><ul>
<li class="chapter" data-level="B.1" data-path="book-dataset-list.html"><a href="book-dataset-list.html#正文案例数据"><i class="fa fa-check"></i><b>B.1</b> 正文案例数据</a></li>
<li class="chapter" data-level="B.2" data-path="book-dataset-list.html"><a href="book-dataset-list.html#习题数据"><i class="fa fa-check"></i><b>B.2</b> 习题数据</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">《属性数据分析》代码</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="intro" class="section level1">
<h1><span class="header-section-number">第 1 章</span> 导言</h1>
<div id="data-intro" class="section level2">
<h2><span class="header-section-number">1.1</span> 属性响应数据</h2>
</div>
<div id="prob-dist" class="section level2">
<h2><span class="header-section-number">1.2</span> 属性数据的概率分布</h2>
<div id="二项分布计算" class="section level3 unnumbered">
<h3>二项分布计算</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co"># 二项分布概率的计算</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="kw">dbinom</span>(<span class="dv">0</span>, <span class="dv">10</span>, <span class="fl">0.2</span>)  <span class="co"># 10次试验，每次成功概率0.2，成功0次</span></a></code></pre></div>
<pre><code>## [1] 0.1074</code></pre>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="co"># 给定参数的情况下批量累积概率</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">n &lt;-<span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">prob_matrix &lt;-<span class="st"> </span><span class="kw">sapply</span>(<span class="kw">c</span>(<span class="fl">0.2</span>, <span class="fl">0.5</span>, <span class="fl">0.8</span>), <span class="cf">function</span>(p) <span class="kw">pbinom</span>(<span class="dv">0</span><span class="op">:</span>n, n, p))</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="kw">dimnames</span>(prob_matrix) &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dv">0</span><span class="op">:</span>n, <span class="kw">c</span>(<span class="st">&quot;P=0.2&quot;</span>, <span class="st">&quot;P=0.5&quot;</span>, <span class="st">&quot;P=0.8&quot;</span>))</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">xtable<span class="op">::</span><span class="kw">xtable</span>(prob_matrix, <span class="dt">align =</span> <span class="st">&quot;cccc&quot;</span>, <span class="dt">digits =</span> <span class="dv">3</span>)</a></code></pre></div>
<table>
<tr>
<th>
P=0.2
</th>
<th>
P=0.5
</th>
<th>
P=0.8
</th>
</tr>
<tr>
<td align="center">
0.107
</td>
<td align="center">
0.001
</td>
<td align="center">
0.000
</td>
</tr>
<tr>
<td align="center">
0.376
</td>
<td align="center">
0.011
</td>
<td align="center">
0.000
</td>
</tr>
<tr>
<td align="center">
0.678
</td>
<td align="center">
0.055
</td>
<td align="center">
0.000
</td>
</tr>
<tr>
<td align="center">
0.879
</td>
<td align="center">
0.172
</td>
<td align="center">
0.001
</td>
</tr>
<tr>
<td align="center">
0.967
</td>
<td align="center">
0.377
</td>
<td align="center">
0.006
</td>
</tr>
<tr>
<td align="center">
0.994
</td>
<td align="center">
0.623
</td>
<td align="center">
0.033
</td>
</tr>
<tr>
<td align="center">
0.999
</td>
<td align="center">
0.828
</td>
<td align="center">
0.121
</td>
</tr>
<tr>
<td align="center">
1.000
</td>
<td align="center">
0.945
</td>
<td align="center">
0.322
</td>
</tr>
<tr>
<td align="center">
1.000
</td>
<td align="center">
0.989
</td>
<td align="center">
0.624
</td>
</tr>
<tr>
<td align="center">
1.000
</td>
<td align="center">
0.999
</td>
<td align="center">
0.893
</td>
</tr>
<tr>
<td align="center">
1.000
</td>
<td align="center">
1.000
</td>
<td align="center">
1.000
</td>
</tr>
</table>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="co"># 给定参数的二项分布的均值和标准差</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">n &lt;-<span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">p &lt;-<span class="st"> </span><span class="fl">0.2</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">n <span class="op">*</span><span class="st"> </span>p  <span class="co"># 均值</span></a></code></pre></div>
<pre><code>## [1] 2</code></pre>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="kw">sqrt</span>(n <span class="op">*</span><span class="st"> </span>p <span class="op">*</span><span class="st"> </span>(<span class="dv">1</span> <span class="op">-</span><span class="st"> </span>p))  <span class="co"># 标准差</span></a></code></pre></div>
<pre><code>## [1] 1.265</code></pre>
</div>
</div>
<div id="stat-infer" class="section level2">
<h2><span class="header-section-number">1.3</span> 比例的统计推断</h2>
<div id="二项分布似然函数图" class="section level3 unnumbered">
<h3>二项分布似然函数图</h3>
<p>书本上的图1.1</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">prob &lt;-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="fl">0.01</span>)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">prob_plot_data &lt;-<span class="st"> </span><span class="kw">data.frame</span>(</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">  <span class="dt">Prob =</span> prob,</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">  <span class="dt">Y_0 =</span> <span class="kw">dbinom</span>(<span class="dv">0</span>, <span class="dv">10</span>, prob),</a>
<a class="sourceLine" id="cb8-5" data-line-number="5">  <span class="dt">Y_6 =</span> <span class="kw">dbinom</span>(<span class="dv">6</span>, <span class="dv">10</span>, prob)</a>
<a class="sourceLine" id="cb8-6" data-line-number="6">)</a>
<a class="sourceLine" id="cb8-7" data-line-number="7"></a>
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="kw">par</span>(<span class="dt">pty =</span> <span class="st">&quot;s&quot;</span>)</a>
<a class="sourceLine" id="cb8-9" data-line-number="9"><span class="kw">plot</span>(</a>
<a class="sourceLine" id="cb8-10" data-line-number="10">  Y_<span class="dv">0</span> <span class="op">~</span><span class="st"> </span>Prob, <span class="dt">type =</span> <span class="st">&quot;l&quot;</span>, </a>
<a class="sourceLine" id="cb8-11" data-line-number="11">  <span class="dt">data =</span> prob_plot_data, </a>
<a class="sourceLine" id="cb8-12" data-line-number="12">  <span class="dt">asp =</span> <span class="dv">1</span>, </a>
<a class="sourceLine" id="cb8-13" data-line-number="13">  <span class="dt">xlab =</span> <span class="st">&quot;Binomial parameter π&quot;</span>,</a>
<a class="sourceLine" id="cb8-14" data-line-number="14">  <span class="dt">ylab =</span> <span class="st">&quot;Likelihood&quot;</span></a>
<a class="sourceLine" id="cb8-15" data-line-number="15">)</a>
<a class="sourceLine" id="cb8-16" data-line-number="16"><span class="kw">lines</span>(Y_<span class="dv">6</span> <span class="op">~</span><span class="st"> </span>Prob, <span class="dt">type =</span> <span class="st">&quot;l&quot;</span>, <span class="dt">data =</span> prob_plot_data)</a></code></pre></div>
<p><img src="cdacode_files/figure-html/unnamed-chunk-5-1.png" width="70%" style="display: block; margin: auto;" /></p>
</div>
<div id="二项分布假设检验" class="section level3 unnumbered">
<h3>二项分布假设检验</h3>
<p>二项分布的检验分为两种，以下例子使用的数据来自1.3.3节的堕胎合法化调查</p>
<p>一种是精确的二项检验，使用<code>binom.test()</code></p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="kw">binom.test</span>(<span class="dv">400</span>, <span class="dv">893</span>)</a></code></pre></div>
<pre><code>## 
##  Exact binomial test
## 
## data:  400 and 893
## number of successes = 400, number of trials = 890,
## p-value = 0.002
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.4150 0.4812
## sample estimates:
## probability of success 
##                 0.4479</code></pre>
<p>另一种是正态（或卡方）近似的二项检验，可使用<code>prob.test()</code></p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="kw">prop.test</span>(<span class="dv">400</span>, <span class="dv">893</span>)</a></code></pre></div>
<pre><code>## 
##  1-sample proportions test with continuity correction
## 
## data:  400 out of 893, null probability 0.5
## X-squared = 9.5, df = 1, p-value = 0.002
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.4151 0.4813
## sample estimates:
##      p 
## 0.4479</code></pre>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="co"># correst=FALSE表示不做连续性调整</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="kw">prop.test</span>(<span class="dv">400</span>, <span class="dv">893</span>, <span class="dt">correct =</span> <span class="ot">FALSE</span>)</a></code></pre></div>
<pre><code>## 
##  1-sample proportions test without continuity
##  correction
## 
## data:  400 out of 893, null probability 0.5
## X-squared = 9.7, df = 1, p-value = 0.002
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.4156 0.4807
## sample estimates:
##      p 
## 0.4479</code></pre>
<p>1.3.2节和1.3.3节介绍和使用的是未经连续性调整的大样本近似</p>
<p>三个检验的p值都小于0.05，从而拒绝原假设</p>
</div>
<div id="二项分布置信区间" class="section level3 unnumbered">
<h3>二项分布置信区间</h3>
<p>上一部分<a href="#binom-test">二项分布假设检验</a>输出的结果中已包含置信区间</p>
<p>其中<code>prop.test(correct = FALSE)</code>输出的是书中介绍的第一种调整方法计算的置信区间</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="kw">prop.test</span>(<span class="dv">9</span>, <span class="dv">10</span>, <span class="fl">0.9</span>, <span class="dt">correct =</span> <span class="ot">FALSE</span>)<span class="op">$</span>conf.int</a></code></pre></div>
<pre><code>## Warning in prop.test(9, 10, 0.9, correct = FALSE): Chi-
## squared approximation may be incorrect</code></pre>
<pre><code>## [1] 0.5958 0.9821
## attr(,&quot;conf.level&quot;)
## [1] 0.95</code></pre>
<p>而对于第二种调整方法，也就是Agresti–Coull confidence interval，R没有自带的函数可以计算，但可以通过<code>binom</code>包中的<code>binom.agresti.coull()</code>函数计算（同时，也可以使用<code>binom.confint()</code>函数计算多种置信区间的汇总表）</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="kw">library</span>(binom)</a>
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="kw">binom.agresti.coull</span>(<span class="dv">9</span>, <span class="dv">10</span>)</a></code></pre></div>
<pre><code>##          method x  n mean lower upper
## 1 agresti-coull 9 10  0.9 0.574 1.004</code></pre>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="kw">binom.confint</span>(<span class="dv">9</span>, <span class="dv">10</span>)</a></code></pre></div>
<pre><code>##           method x  n   mean  lower  upper
## 1  agresti-coull 9 10 0.9000 0.5740 1.0039
## 2     asymptotic 9 10 0.9000 0.7141 1.0859
## 3          bayes 9 10 0.8636 0.6692 0.9996
## 4        cloglog 9 10 0.9000 0.4730 0.9853
## 5          exact 9 10 0.9000 0.5550 0.9975
## 6          logit 9 10 0.9000 0.5328 0.9861
## 7         probit 9 10 0.9000 0.5879 0.9904
## 8        profile 9 10 0.9000 0.6283 0.9904
## 9            lrt 9 10 0.9000 0.6284 0.9940
## 10     prop.test 9 10 0.9000 0.5412 0.9948
## 11        wilson 9 10 0.9000 0.5958 0.9821</code></pre>
</div>
</div>
<div id="more-stat-infer" class="section level2">
<h2><span class="header-section-number">1.4</span> 关于离散数据的更多统计推断</h2>
<div id="二项分布参数统计推断" class="section level3 unnumbered">
<h3>二项分布参数统计推断</h3>
<p>对于Wald, Score, and Likelihood-Ratio这三种推断方法</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="co"># 参数设定</span></a>
<a class="sourceLine" id="cb22-2" data-line-number="2">p &lt;-<span class="st"> </span><span class="fl">0.9</span></a>
<a class="sourceLine" id="cb22-3" data-line-number="3">n &lt;-<span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb22-4" data-line-number="4">pi &lt;-<span class="st"> </span><span class="fl">0.5</span></a></code></pre></div>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="co"># Wald test</span></a>
<a class="sourceLine" id="cb23-2" data-line-number="2">SE &lt;-<span class="st"> </span><span class="kw">sqrt</span>(p <span class="op">*</span><span class="st"> </span>(<span class="dv">1</span> <span class="op">-</span><span class="st"> </span>p) <span class="op">/</span><span class="st"> </span>n)</a>
<a class="sourceLine" id="cb23-3" data-line-number="3">z &lt;-<span class="st"> </span>(p <span class="op">-</span><span class="st"> </span>pi) <span class="op">/</span><span class="st"> </span>SE; z</a></code></pre></div>
<pre><code>## [1] 4.216</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="co"># Score test</span></a>
<a class="sourceLine" id="cb25-2" data-line-number="2">SE &lt;-<span class="st"> </span><span class="kw">sqrt</span>(pi <span class="op">*</span><span class="st"> </span>(<span class="dv">1</span> <span class="op">-</span><span class="st"> </span>pi) <span class="op">/</span><span class="st"> </span>n)</a>
<a class="sourceLine" id="cb25-3" data-line-number="3">z &lt;-<span class="st"> </span>(p <span class="op">-</span><span class="st"> </span>pi) <span class="op">/</span><span class="st"> </span>SE; z</a></code></pre></div>
<pre><code>## [1] 2.53</code></pre>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb27-1" data-line-number="1"><span class="co"># likelihood-ratio test</span></a>
<a class="sourceLine" id="cb27-2" data-line-number="2">x &lt;-<span class="st"> </span>n <span class="op">*</span><span class="st"> </span>p</a>
<a class="sourceLine" id="cb27-3" data-line-number="3">L0 &lt;-<span class="st"> </span><span class="kw">dbinom</span>(x, n, pi)</a>
<a class="sourceLine" id="cb27-4" data-line-number="4">L1 &lt;-<span class="st"> </span><span class="kw">dbinom</span>(x, n, p)</a>
<a class="sourceLine" id="cb27-5" data-line-number="5">z &lt;-<span class="st"> </span><span class="dv">-2</span> <span class="op">*</span><span class="st"> </span><span class="kw">log</span>(L0 <span class="op">/</span><span class="st"> </span>L1); z</a></code></pre></div>
<pre><code>## [1] 7.361</code></pre>
<p>或者可以使用<code>cdabookcode</code>中定义的<code>binom_inference()</code>函数计算</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb29-2" data-line-number="2"><span class="kw">binom_inference</span>(<span class="fl">0.9</span>, <span class="dv">10</span>, <span class="fl">0.5</span>, <span class="dt">method =</span> <span class="st">&quot;wald&quot;</span>)</a></code></pre></div>
<pre><code>## $z
## [1] 4.216
## 
## $method
## [1] &quot;wald&quot;</code></pre>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="kw">binom_inference</span>(<span class="fl">0.9</span>, <span class="dv">10</span>, <span class="fl">0.5</span>, <span class="dt">method =</span> <span class="st">&quot;l&quot;</span>)</a></code></pre></div>
<pre><code>## $z
## [1] 7.361
## 
## $method
## [1] &quot;likelihood-ratio test&quot;</code></pre>
</div>
<div id="小样本推断" class="section level3 unnumbered">
<h3>小样本推断</h3>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb33-1" data-line-number="1"><span class="co"># one-side test pvalue</span></a>
<a class="sourceLine" id="cb33-2" data-line-number="2"><span class="co"># (H0: pi = 0.5) vs (H1: pi &gt; 0.5) </span></a>
<a class="sourceLine" id="cb33-3" data-line-number="3"><span class="co"># p-value = P(Y &gt;= 9) = P(Y &gt; 8)</span></a>
<a class="sourceLine" id="cb33-4" data-line-number="4"><span class="dv">1</span> <span class="op">-</span><span class="st"> </span><span class="kw">pbinom</span>(<span class="dv">8</span>, <span class="dv">10</span>, <span class="fl">0.5</span>)</a></code></pre></div>
<pre><code>## [1] 0.01074</code></pre>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb35-1" data-line-number="1"><span class="co"># two-side test pvalue</span></a>
<a class="sourceLine" id="cb35-2" data-line-number="2"><span class="co"># (H0: pi = 0.5) vs (H1: pi != 0.5)</span></a>
<a class="sourceLine" id="cb35-3" data-line-number="3"><span class="co"># p-value = 1 + P(Y &lt;= 1) + P(Y &gt;= 9) = 2 * P(Y &gt; 8)</span></a>
<a class="sourceLine" id="cb35-4" data-line-number="4"><span class="kw">pbinom</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="fl">0.5</span>) <span class="op">+</span><span class="st"> </span><span class="kw">pbinom</span>(<span class="dv">8</span>, <span class="dv">10</span>, <span class="fl">0.5</span>, <span class="dt">lower.tail =</span> <span class="ot">FALSE</span>)</a></code></pre></div>
<pre><code>## [1] 0.02148</code></pre>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb37-1" data-line-number="1"><span class="dv">2</span> <span class="op">*</span><span class="st"> </span>(<span class="dv">1</span> <span class="op">-</span><span class="st"> </span><span class="kw">pbinom</span>(<span class="dv">8</span>, <span class="dv">10</span>, <span class="fl">0.5</span>))</a></code></pre></div>
<pre><code>## [1] 0.02148</code></pre>
</div>
<div id="小样本推断p值调整" class="section level3 unnumbered">
<h3>小样本推断P值调整</h3>
<p>小样本推断是保守的，可以使用经过调整的p值</p>
<p>中点P值可使用<code>binom_mid_pvalue()</code>计算</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb39-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb39-2" data-line-number="2"><span class="kw">binom_mid_pvalue</span>(<span class="dv">9</span>, <span class="dv">10</span>, <span class="st">&quot;g&quot;</span>)  <span class="co"># right-tail p-value</span></a></code></pre></div>
<pre><code>## $pvalue
## [1] 0.005859
## 
## $alternative
## [1] &quot;greater&quot;</code></pre>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb41-1" data-line-number="1"><span class="kw">binom_mid_pvalue</span>(<span class="dv">9</span>, <span class="dv">10</span>)  <span class="co"># two-sided p-value</span></a></code></pre></div>
<pre><code>## $pvalue
## [1] 0.01172
## 
## $alternative
## [1] &quot;two.sided&quot;</code></pre>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb43-1" data-line-number="1"><span class="co"># 获取表1.2</span></a>
<a class="sourceLine" id="cb43-2" data-line-number="2">pvalue_matrix &lt;-<span class="st"> </span><span class="kw">cbind</span>(</a>
<a class="sourceLine" id="cb43-3" data-line-number="3">  <span class="dv">0</span><span class="op">:</span><span class="dv">10</span>,</a>
<a class="sourceLine" id="cb43-4" data-line-number="4">  <span class="kw">dbinom</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">10</span>, <span class="dv">10</span>, <span class="fl">0.5</span>),</a>
<a class="sourceLine" id="cb43-5" data-line-number="5">  <span class="dv">1</span> <span class="op">-</span><span class="st"> </span><span class="kw">pbinom</span>(<span class="op">-</span><span class="dv">1</span><span class="op">:</span><span class="dv">9</span>, <span class="dv">10</span>, <span class="fl">0.5</span>),</a>
<a class="sourceLine" id="cb43-6" data-line-number="6">  <span class="kw">binom_mid_pvalue</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">10</span>, <span class="dv">10</span>, <span class="st">&quot;g&quot;</span>)<span class="op">$</span>pvalue</a>
<a class="sourceLine" id="cb43-7" data-line-number="7">)</a>
<a class="sourceLine" id="cb43-8" data-line-number="8"><span class="kw">dimnames</span>(pvalue_matrix) &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">10</span>, <span class="kw">c</span>(<span class="st">&quot;y&quot;</span>, <span class="st">&quot;P(y)&quot;</span>, <span class="st">&quot;P-value&quot;</span>, <span class="st">&quot;Mid P-value&quot;</span>))</a>
<a class="sourceLine" id="cb43-9" data-line-number="9">xtable<span class="op">::</span><span class="kw">xtable</span>(pvalue_matrix, <span class="dt">align =</span> <span class="st">&quot;ccccc&quot;</span>, <span class="dt">digits =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">4</span>, <span class="dv">4</span>, <span class="dv">4</span>))</a></code></pre></div>
<table>
<tr>
<th>
y
</th>
<th>
P(y)
</th>
<th>
P-value
</th>
<th>
Mid P-value
</th>
</tr>
<tr>
<td align="center">
0
</td>
<td align="center">
0.0010
</td>
<td align="center">
1.0000
</td>
<td align="center">
0.9995
</td>
</tr>
<tr>
<td align="center">
1
</td>
<td align="center">
0.0098
</td>
<td align="center">
0.9990
</td>
<td align="center">
0.9941
</td>
</tr>
<tr>
<td align="center">
2
</td>
<td align="center">
0.0439
</td>
<td align="center">
0.9893
</td>
<td align="center">
0.9673
</td>
</tr>
<tr>
<td align="center">
3
</td>
<td align="center">
0.1172
</td>
<td align="center">
0.9453
</td>
<td align="center">
0.8867
</td>
</tr>
<tr>
<td align="center">
4
</td>
<td align="center">
0.2051
</td>
<td align="center">
0.8281
</td>
<td align="center">
0.7256
</td>
</tr>
<tr>
<td align="center">
5
</td>
<td align="center">
0.2461
</td>
<td align="center">
0.6230
</td>
<td align="center">
0.5000
</td>
</tr>
<tr>
<td align="center">
6
</td>
<td align="center">
0.2051
</td>
<td align="center">
0.3770
</td>
<td align="center">
0.2744
</td>
</tr>
<tr>
<td align="center">
7
</td>
<td align="center">
0.1172
</td>
<td align="center">
0.1719
</td>
<td align="center">
0.1133
</td>
</tr>
<tr>
<td align="center">
8
</td>
<td align="center">
0.0439
</td>
<td align="center">
0.0547
</td>
<td align="center">
0.0327
</td>
</tr>
<tr>
<td align="center">
9
</td>
<td align="center">
0.0098
</td>
<td align="center">
0.0107
</td>
<td align="center">
0.0059
</td>
</tr>
<tr>
<td align="center">
10
</td>
<td align="center">
0.0010
</td>
<td align="center">
0.0010
</td>
<td align="center">
0.0005
</td>
</tr>
</table>
</div>
</div>
<div id="problems-ch1" class="section level2 unnumbered">
<h2>课后题</h2>
<div id="第4题" class="section level3 unnumbered">
<h3>第4题</h3>
<ol style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb44-1" data-line-number="1"><span class="co"># (a)</span></a>
<a class="sourceLine" id="cb44-2" data-line-number="2">pi &lt;-<span class="st"> </span><span class="fl">0.5</span></a>
<a class="sourceLine" id="cb44-3" data-line-number="3"></a>
<a class="sourceLine" id="cb44-4" data-line-number="4">result &lt;-<span class="st"> </span><span class="kw">dbinom</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">2</span>, <span class="dv">2</span>, <span class="fl">0.5</span>)</a>
<a class="sourceLine" id="cb44-5" data-line-number="5"><span class="kw">names</span>(result) &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;P(Y=&quot;</span>, <span class="dv">0</span><span class="op">:</span><span class="dv">2</span>, <span class="st">&quot;)&quot;</span>)</a>
<a class="sourceLine" id="cb44-6" data-line-number="6">result</a></code></pre></div>
<pre><code>## P(Y=0) P(Y=1) P(Y=2) 
##   0.25   0.50   0.25</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb46-1" data-line-number="1"><span class="dv">2</span> <span class="op">*</span><span class="st"> </span><span class="fl">0.5</span>  <span class="co"># 均值</span></a></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb48-1" data-line-number="1"><span class="kw">sqrt</span>(<span class="dv">2</span> <span class="op">*</span><span class="st"> </span><span class="fl">0.5</span> <span class="op">*</span><span class="st"> </span><span class="fl">0.5</span>)  <span class="co"># 标准差</span></a></code></pre></div>
<pre><code>## [1] 0.7071</code></pre>
<ol start="2" style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb50-1" data-line-number="1"><span class="co"># (b)(i)</span></a>
<a class="sourceLine" id="cb50-2" data-line-number="2"><span class="kw">dbinom</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">2</span>, <span class="dv">2</span>, <span class="fl">0.6</span>)</a></code></pre></div>
<pre><code>## [1] 0.16 0.48 0.36</code></pre>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb52-1" data-line-number="1"><span class="co"># (b)(ii)</span></a>
<a class="sourceLine" id="cb52-2" data-line-number="2"><span class="kw">dbinom</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">2</span>, <span class="dv">2</span>, <span class="fl">0.4</span>)</a></code></pre></div>
<pre><code>## [1] 0.36 0.48 0.16</code></pre>
<ol start="3" style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb54-1" data-line-number="1">prob &lt;-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="fl">0.01</span>)</a>
<a class="sourceLine" id="cb54-2" data-line-number="2">prob_plot_data &lt;-<span class="st"> </span><span class="kw">data.frame</span>(</a>
<a class="sourceLine" id="cb54-3" data-line-number="3">  <span class="dt">Prob =</span> prob,</a>
<a class="sourceLine" id="cb54-4" data-line-number="4">  <span class="dt">Y_1 =</span> <span class="kw">dbinom</span>(<span class="dv">1</span>, <span class="dv">2</span>, prob)</a>
<a class="sourceLine" id="cb54-5" data-line-number="5">)</a>
<a class="sourceLine" id="cb54-6" data-line-number="6"></a>
<a class="sourceLine" id="cb54-7" data-line-number="7"><span class="kw">plot</span>(</a>
<a class="sourceLine" id="cb54-8" data-line-number="8">  Y_<span class="dv">1</span> <span class="op">~</span><span class="st"> </span>Prob, <span class="dt">type =</span> <span class="st">&quot;l&quot;</span>, </a>
<a class="sourceLine" id="cb54-9" data-line-number="9">  <span class="dt">data =</span> prob_plot_data, </a>
<a class="sourceLine" id="cb54-10" data-line-number="10">  <span class="dt">asp =</span> <span class="dv">1</span>, <span class="dt">ylab =</span> <span class="st">&quot;Likelihood&quot;</span>,</a>
<a class="sourceLine" id="cb54-11" data-line-number="11">  <span class="dt">xlim =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>), <span class="dt">ylim =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.5</span>)</a>
<a class="sourceLine" id="cb54-12" data-line-number="12">)</a></code></pre></div>
<p><img src="cdacode_files/figure-html/unnamed-chunk-19-1.png" width="70%" style="display: block; margin: auto;" /></p>
<ol start="4" style="list-style-type: lower-alpha">
<li>根据(c)中的图，likelihood在prob为0.5时达到最大，因此ML估计值为0.5</li>
</ol>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="index.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="contingency-table.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook/js/app.min.js"></script>
<script src="libs/gitbook/js/lunr.js"></script>
<script src="libs/gitbook/js/plugin-search.js"></script>
<script src="libs/gitbook/js/plugin-sharing.js"></script>
<script src="libs/gitbook/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook/js/plugin-bookdown.js"></script>
<script src="libs/gitbook/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"google": false,
"linkedin": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/jinzhen-lin/cdacode-document/edit/master/01-introduction.Rmd",
"text": "编辑"
},
"download": ["cdacode.pdf", "cdacode.epub", "cdacode.zip"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:" && /^https?:/.test(src))
      src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
